package bg.code.commons.plugin.ui;

import bg.code.commons.builder.idea.ProjectPluginConfig;
import bg.code.commons.builder.project.ToolConfigurationData;
import bg.code.commons.builder.utils.Strings;
import bg.code.commons.builder.vo.ColumnDescriptor;
import bg.code.commons.builder.vo.GenerateConfig;
import bg.code.commons.plugin.ui.model.FieldTableModel;
import com.intellij.openapi.ui.FrameWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.ui.components.*;
import com.intellij.ui.components.panels.VerticalBox;
import com.intellij.ui.table.JBTable;

import javax.swing.*;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import java.awt.*;
import java.util.List;

import static bg.code.commons.plugin.Cons.*;

/**
 * @author 郑兴泉 956607644@qq.com
 * @data 2020/3/19
 * 描述：
 */
public class PreviewData3 extends FrameWrapper {

    private ProjectPluginConfig pluginConfig;
    private JTable fieldTable;
    private GenerateConfig generateConfig;

    private final int width;
    private final int height;

    public PreviewData3(ProjectPluginConfig pluginConfig, GenerateConfig generateConfig) {
        super(pluginConfig.getProject());
        this.pluginConfig = pluginConfig;
        this.generateConfig = generateConfig;
        this.width = 1600;
        this.height = 800;
        this.setupViews();
        this.setSize(new Dimension(width, height));
        this.setTitle(GENERATOR_TITLE_NAME);
        this.setProject(pluginConfig.getProject());
        this.closeOnEsc();

        this.buildData();
        this.setComponent(genComponent());


    }

    private JComponent genComponent() {
        JSplitPane pane = new JSplitPane();
        pane.setLeftComponent(genLeftComponent());
        pane.setRightComponent(genRightComponent());




        VerticalBox root = new VerticalBox();
        root.add(pane);

        this.fieldTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        JScrollPane scrollPane3 = new JBScrollPane(this.fieldTable);
        root.add(scrollPane3);
        return root;
    }

    private Component genRightComponent() {


        JBPanel panel = new JBPanel();

        panel.setLayout(new GridLayout(8,2));

        JBLabel saveMethodName = new JBLabel(SAVE_METHOD_NAME);
        JBLabel editMethodName = new JBLabel(EDIT_METHOD_NAME);
        JBLabel listMethodName = new JBLabel(LIST_METHOD_NAME);
        JBLabel deleteMethodName = new JBLabel(DELETE_METHOD_NAME);
        JBLabel enabledMethodName = new JBLabel(ENABLED_METHOD_NAME);
        JBLabel disabledMethodName = new JBLabel(DISABLED_METHOD_NAME);
        JBLabel exportMethodName = new JBLabel(EXPORT_METHOD_NAME);

        JBTextField saveMethodNameTextField = new JBTextField();
        JBTextField editMethodNameTextField = new JBTextField();
        JBTextField listMethodNameTextField = new JBTextField();
        JBTextField deleteMethodNameTextField = new JBTextField();
        JBTextField enabledMethodNameTextField = new JBTextField();
        JBTextField disabledMethodNameTextField = new JBTextField();
        JBTextField exportMethodNameTextField = new JBTextField();


        panel.add(saveMethodName);
        panel.add(saveMethodNameTextField);
        panel.add(editMethodName);
        panel.add(editMethodNameTextField);
        panel.add(listMethodName);
        panel.add(listMethodNameTextField);
        panel.add(deleteMethodName);
        panel.add(deleteMethodNameTextField);
        panel.add(enabledMethodName);
        panel.add(enabledMethodNameTextField);
        panel.add(disabledMethodName);
        panel.add(disabledMethodNameTextField);
        panel.add(exportMethodName);
        panel.add(exportMethodNameTextField);

        JScrollPane scrollPane3 = new JBScrollPane(panel);
        return scrollPane3;
    }

    private JComponent genLeftComponent() {

        JBPanel panel = new JBPanel();
        GridLayout layout = new GridLayout(3, 2);
        panel.setLayout(layout);

        JBLabel authorLabel = new JBLabel(AUTHOR_TEXT);
        JBLabel funNameLabel = new JBLabel(FUN_NAME_TEXT);
        JBLabel userMailLabel = new JBLabel(USER_MAIL_TEXT);
        JBTextField authorTextField = new JBTextField();
        JBTextArea funNameTextField = new JBTextArea(3, 4);
        JBTextField userMailTextField = new JBTextField();

        panel.add(authorLabel);
        panel.add(authorTextField);
        panel.add(funNameLabel);
        panel.add(funNameTextField);
        panel.add(userMailLabel);
        panel.add(userMailTextField);

        return panel;
    }

    //    @Override
    protected void doOKAction() {
        ToolConfigurationData configuration = pluginConfig.getConfiguration();
        if (Strings.isBlank(configuration.getTemplatePath())) {
            Messages.showErrorDialog("请先设置模板目录", "错误提示");
            return;
        } else {
//            Path path = Paths.get(configuration.getTemplatePath());
//            File[] list = path.toFile().listFiles();
//            File[] var11 = list;
//            int var12 = list.length;
//
//            for(int var13 = 0; var13 < var12; ++var13) {
//                File file = var11[var13];
//                if (!file.isFile()) {
//                    this.templateSelect.addItem(file.getName());
//                }
//            }
        }
//        super.doOKAction();
    }

    private void setupViews() {


    }

    private void buildData() {
        List<ColumnDescriptor> javaFields = this.pluginConfig.getJavaFields();
        TableModel model = new FieldTableModel(javaFields);
        this.fieldTable = new JBTable();
        this.fieldTable.setShowGrid(true);
        this.fieldTable.setSize(width, height);
        this.fieldTable.setModel(model);
        this.fieldTable.setRowHeight(30);
        changeCellWith();
    }

    public void changeCellWith() {
        TableModel fieldsModel = this.fieldTable.getModel();
        int headersLength = fieldsModel.getColumnCount();
        TableColumnModel columnModel = this.fieldTable.getColumnModel();
        for (int i = 0; i < headersLength; ++i) {
            TableColumn tableColumn = columnModel.getColumn(i);
            Object value = fieldsModel.getValueAt(0, i);
            if (value instanceof Boolean) {
                // 列宽
                tableColumn.setPreferredWidth(100);
            } else if (value instanceof Integer) {
                tableColumn.setPreferredWidth(100);
            } else {
                tableColumn.setPreferredWidth(150);
            }
        }
    }

}
